package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @DubboReference
    private PermissionService permissionService;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pages = permissionService.findPage(queryPageBean);
        if (pages!=null){
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,pages);
        }
        return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
    }

    @GetMapping("/all")
    public Result all(){
        List list = permissionService.all();
        if (list!=null){
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,list);
        }
        return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Permission permission,Integer[] roles){
        int i = permissionService.add(permission,roles);
        if (i==1){
            return new Result(true,MessageConst.ADD_PERMISSION_SUCCESS);
        }
        return new Result(false,MessageConst.ADD_PERMISSION_FAIL);
    }

    @GetMapping("/findByPermissionRoleId")
    public Result findByPermissionRoleId(Integer id){
        List list = permissionService.findByPermissionRoleId(id);
        if (list!=null){
            return new Result(true,MessageConst.QUERY_IDENTITY_SUCCESS,list);
        }
        return new Result(false,MessageConst.QUERY_IDENTITY_FAIL);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        if (permission!=null){
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permission);
        }
        return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Permission permission,Integer[] roles){
        int i = permissionService.edit(permission,roles);
        if (i==10000){
            return new Result(false,"有关联角色 , 无法删除");
        }
        if (i==1){
            return new Result(true,MessageConst.EDIT_PERMISSION_SUCCESS);
        }
        return new Result(false,MessageConst.EDIT_PERMISSION_FAIL);
    }

    @PostMapping("/delete")
    public Result delete(Integer id){
        int i  = permissionService.deleteById(id);
        if (i==1){
            return new Result(true,MessageConst.DELETE_PERMISSION_SUCCESS);
        }
        return new Result(false,MessageConst.DELETE_PERMISSION_FAIL);
    }
}
